<%@ Page Language="C#" Debug="true" ContentType="text/xml" Title="Reviewer.dk - RSS til anmeldelser" %>

<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="Reviewer.Entities" %>
<%@ Import Namespace="Reviewer.Entities.Services" %>
<%@ Import Namespace="Reviewer.Business" %>

<script language="C#" runat="server">

    void Page_load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (IReviewService reviewService = ServiceFactory.ReviewService)
            using (IGameService gameService = ServiceFactory.GameService)
            using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
            {
                XmlTextWriter MyWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

                MyWriter.WriteStartDocument();
                MyWriter.WriteStartElement("rss");
                MyWriter.WriteAttributeString("version", "2.0");
                MyWriter.WriteStartElement("channel");
                MyWriter.WriteElementString("title", "Reviewer.dk - Anmeldelser");
                MyWriter.WriteElementString("description", "Dansk side med daglige nyheder til spil, samt anmeldelser af spil til konsoller og pc");
                MyWriter.WriteElementString("link", "http://www.reviewer.dk");
                MyWriter.WriteElementString("ttl", "60");
                MyWriter.WriteElementString("language", "da");
                MyWriter.WriteElementString("copyright", "Copyright 2008 Reviewer.dk");
                MyWriter.WriteElementString("lastBuildDate", DateTime.Now.ToString());
                MyWriter.WriteStartElement("image");
                MyWriter.WriteElementString("url", "http://www.reviewer.dk/grafik/rsslogo.jpg");
                MyWriter.WriteElementString("link", "http://www.reviewer.dk");
                MyWriter.WriteElementString("title", "Reviewer.dk - Anmeldelser");
                MyWriter.WriteElementString("width", "100");
                MyWriter.WriteElementString("height", "52");
                MyWriter.WriteEndElement();

                foreach (Review review in reviewService.Find().OrderByDescending(t => t.Id))
                {
                    Game game = gameService.GetBy(review.GameId);
                    if (game == null)
                        continue;
                    Format format = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(game.Format));
                    if (format == null)
                        continue;

                    MyWriter.WriteStartElement("item");
                    MyWriter.WriteElementString("title", game.Title + " Anmeldelse til " + (format == null ? "Ukendt format" : format.Name));
                    MyWriter.WriteElementString("category", (format == null ? "Ukendt format" : format.Name));
                    MyWriter.WriteElementString("pubDate", BuildPubDate(review.LastModifiedDate));
                    MyWriter.WriteElementString("description", review.Header);
                    MyWriter.WriteElementString("link", "http://www.reviewer.dk" + UrlUtillity.GetReviewUri(review));
                    MyWriter.WriteEndElement();
                }

                MyWriter.WriteEndElement();
                MyWriter.WriteEndElement();
                MyWriter.WriteEndDocument();
                MyWriter.Flush();
                MyWriter.Close();
            }
        }
    }

    private string BuildPubDate(DateTime d)
    {
        try
        {
            string RV = "";
            string day = d.Day.ToString();
            if (day.Length == 1) { day = "0" + day; }
            string month = d.Month.ToString();
            if (month == "1") { month = "January"; }
            else if (month == "2") { month = "February"; }
            else if (month == "3") { month = "March"; }
            else if (month == "4") { month = "April"; }
            else if (month == "5") { month = "May"; }
            else if (month == "6") { month = "June"; }
            else if (month == "7") { month = "July"; }
            else if (month == "8") { month = "August"; }
            else if (month == "9") { month = "September"; }
            else if (month == "10") { month = "October"; }
            else if (month == "11") { month = "November"; }
            else if (month == "12") { month = "December"; }

            string mTime = "";
            DateTime mDate = d.ToUniversalTime();
            if (mDate.Hour.ToString().Length == 1)
            {
                mTime = "0" + mDate.Hour.ToString();
            }
            else
            {
                mTime = mDate.Hour.ToString();
            }

            mTime += ":";
            if (mDate.Minute.ToString().Length == 1)
            {
                mTime += "0" + mDate.Minute.ToString();
            }
            else
            {
                mTime += mDate.Minute.ToString();
            }

            mTime += ":";
            if (mDate.Second.ToString().Length == 1)
            {
                mTime += "0" + mDate.Second.ToString();
            }
            else
            {
                mTime += mDate.Second.ToString();
            }

            RV = d.DayOfWeek.ToString().Substring(0, 3);
            RV += ", " + day + " " + month.Substring(0, 3);
            RV += " " + d.Year.ToString() + " " + mTime + " GMT";
            return RV;

        }
        catch (Exception)
        {
            return null;
        }
    }
    
</script>

